package algorithm_demo;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Api
 * @date 2023/9/6 0:30
 */
public class Test {

    public static void main(String[] args) {
        //想要处理的文本 a.b.C.d.e.1
        String str = "a.b.C.d.e.1";

        //创建正则表达式对象,制定好匹配规则 [a-z]
        Pattern p1 = Pattern.compile("[a-z]");

        //创建正则表达式对象,制定好匹配规则 [A-Z]
        Pattern p2 = Pattern.compile("[A-Z]");

        //创建正则表达式对象,制定好匹配规则 [0-9]
        Pattern p3 = Pattern.compile("[0-9]");

        //创建正则表达式对象,制定好匹配规则 [b-d]
        Pattern p4 = Pattern.compile("[b-d]");

        //创建正则表达式对象,制定好匹配规则 [d-b],定义是会报错
        //Pattern p5 = Pattern.compile("[d-b]");

        //创建匹配器,将正则表达式对象和想要处理的文本关联起来
        Matcher m1 = p1.matcher(str);
        Matcher m2 = p2.matcher(str);
        Matcher m3 = p3.matcher(str);
        Matcher m4 = p4.matcher(str);
        //开始匹配
        System.out.print("[a-z]的匹配结果:");
        while (m1.find()) {
            //输出匹配的结果
            System.out.print(m1.group(0) + " ");
        }
        System.out.println();
        System.out.print("[A-Z]的匹配结果:");
        while (m2.find()) {
            //输出匹配的结果
            System.out.print(m2.group(0) + " ");
        }
        System.out.println();
        System.out.print("[0-9]的匹配结果:");
        while (m3.find()) {
            //输出匹配的结果
            System.out.print(m3.group(0) + " ");
        }
        System.out.println();
        System.out.print("[b-d]的匹配结果:");
        while (m4.find()) {
            //输出匹配的结果
            System.out.print(m4.group(0) + " ");
        }
    }
}
